package com.lxg.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @author 陆小根
 * date: 2022/04/26
 * Description:
 */
@Component
@Aspect
public class MyAdvice {

  @Pointcut("execution(* com.lxg.dao.BookDao.findName(..))")
  private void pt(){}

//  @Before("pt()")
  public void before(JoinPoint jp) {
    // 获取参数
    Object[] args = jp.getArgs();
    System.out.println(Arrays.toString(args));
    System.out.println("before advice ...");
  }

//  @After("pt()")
  public void after(JoinPoint jp) {
    Object[] args = jp.getArgs();
    System.out.println(Arrays.toString(args));
    System.out.println("after advice ...");
  }

//  @Around("pt()")
  public Object around(ProceedingJoinPoint pjp) {
    Object[] args = pjp.getArgs();
    args[0] = 666;
    System.out.println(Arrays.toString(args));
    Object ret = null;
    try {
      ret = pjp.proceed(args);
    } catch (Throwable e) {
      e.printStackTrace();
    }
    return ret;
  }

//  @AfterReturning(value = "pt()", returning = "ret")
  public void afterReturning(JoinPoint jp, String ret) {
    System.out.println(ret);
    System.out.println("afterReturning advice...");
  }

  @AfterThrowing(value = "pt()", throwing = "e")
  public void afterThrowing(Throwable e) {
    System.out.println(e);
    System.out.println("afterThrowing advice ...");
  }


}
